package com.loopy.tomcat.handler;

import com.loopy.tomcat.TomcatV03;
import com.loopy.tomcat.http.loopyRequest;
import com.loopy.tomcat.http.loopyResponse;
import com.loopy.tomcat.servlet.loopyhttpSevlet;
import com.loopy.tomcat.utils.WebUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class RequestHandler implements Runnable {
    private Socket socket = null;

    public RequestHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
           /*  InputStream inputStream = socket.getInputStream();
           BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String msg="";
            System.out.println("当前线程名"+Thread.currentThread().getName());
            System.out.println("================TomcatV02接收到浏览器发送数据================");
            while ((msg=br.readLine())!=null){
                if (msg.length()==0){
                    break;
                }
                System.out.println(msg);
            }*/
            loopyRequest request = new loopyRequest(socket.getInputStream());


            loopyResponse response = new loopyResponse(socket.getOutputStream());
/*          String res=loopyResponse.resHeader+"hi loopy欢迎来中国";
            System.out.println("==========给浏览器发送的数据是===========");
            System.out.println(res);
            OutputStream outputStream = loopyResponse.getOutputStream();
            outputStream.write(res.getBytes());
            outputStream.flush();
            outputStream.close();*/

//            loopyCalServlet loopyCalServlet = new loopyCalServlet();
//            loopyCalServlet.doGet(loopyRequest,loopyResponse);
            String uri = request.getUri();
//            System.out.println(uri);

            if (WebUtils.isHtml(uri)) {
//                System.out.println("进入html");
               /* String path = RequestHandler.class.getResource("/").getPath();
                StringBuilder stringBuilder=new StringBuilder();
                BufferedReader bufferedReader = new BufferedReader(new FileReader(path + uri.substring(1)));
                System.out.println(path+uri.substring(1));
                String msg="";
                while ((msg=bufferedReader.readLine())!=null){
                    stringBuilder.append(msg);
                }
                msg=stringBuilder.toString();
                System.out.println(msg);*/

                String msg = WebUtils.readHtml(uri.substring(1));
                msg = loopyResponse.resHeader+msg;
//                System.out.println(msg);
                OutputStream outputStream = response.getOutputStream();
                outputStream.write(msg.getBytes());
                outputStream.flush();
                outputStream.close();
                socket.close();
                return;
            }
            String servletName = TomcatV03.servletUrlMapping.get(uri);
            if (servletName == null) {
                servletName = "";
            }

            loopyhttpSevlet sevlet = TomcatV03.servletMapping.get(servletName);

            if (sevlet != null) {
                sevlet.service(request, response);
            } else {
                String resp = loopyResponse.resHeader + "<h1>" + 404 + "</h1>";
                OutputStream outputStream = response.getOutputStream();
                outputStream.write(resp.getBytes());
                outputStream.flush();
                outputStream.close();
            }

//            inputStream.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
